﻿#if !COREFX
using System;
using System.Data;

namespace XW.Data
{
	internal class QueryDataSetHandler<TEntity> : IQueryTypeHandler<TEntity>
	{
		private readonly DbCommandData _data;

		public bool IterateDataReader { get { return false; } }

		public QueryDataSetHandler(DbCommandData data)
		{
			_data = data;
		}

		public object HandleType(Action<TEntity, IDataReader> customMapperReader, Action<TEntity, dynamic> customMapperDynamic)
		{ 
			return DataReader2DataSet(_data.Reader.InnerReader); 
		}

		private DataSet DataReader2DataSet(System.Data.IDataReader reader) {

			DataSet ds = new DataSet();
			int i = 0;
			while (!reader.IsClosed)
			{
				ds.Tables.Add("Table" + (i + 1));
				ds.EnforceConstraints = false;
				ds.Tables[i].Load(reader);
				i++;
			}
			return ds;
		}
	}
}
#endif
